<?php
/**
 * Created by PhpStorm.
 * User: user1
 * Date: 2018/5/16
 * Time: 17:20
 */

namespace app\common\service;


use think\Exception;
use think\facade\Request;

class UploadService
{
    /**
     * 图片上传接口
     * @return array
     */
    public static function upload_image() {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( './uploads');
        if($info){

            $path = '/uploads/' . str_replace('\\', '/', $info->getSaveName());
//            $image = \think\Image::open('.' . $path);
            // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png

//            $ext = $info->getExtension();
//            $thumb_path =  str_replace('.' . $ext, '-thumb.' . $ext, $path);

//            $image->thumb(750, 750)->save('.' . $thumb_path);

            $path = Request::domain() . $path;
            return ['code' => 0, 'msg' => '上传成功', 'path' => $path];
        }else{
            // 上传失败获取错误信息
            return ['code' => 1, 'msg' => $file->getError(), 'path' => ''];
        }
    }

    /**
     * 上传文件
     * @return array
     */
    public static function upload_file() {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');

        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( './uploads/file');
        if($info){
            $dir_path = '/uploads/file/' . $info->getSaveName();
            $path = Request::domain() . $dir_path;
            return ['code' => 0, 'msg' => '上传成功', 'data' => ['src' => $path, 'dir_path' => $dir_path]];
        }else{
            // 上传失败获取错误信息
            return ['code' => 1, 'msg' => $file->getError(), 'data' => []];
        }
    }
}